package com.uwantsoft.business.service.domain.user;

import com.uwantsoft.business.facade.dto.user.LicensePlateDto;
import com.uwantsoft.util.AbstractEntity;

import javax.persistence.*;

/**
 * @author LeiPeichen.
 */
@Entity
@Table(name = "licenseplate")
public class LicensePlate extends AbstractEntity {
    @ManyToOne(targetEntity = Driver.class)
    @JoinColumn(name = "driver_id")
    private Driver driver;
    @Column(name = "license_plate_number")
    private String licensePlateNumber;
    @Column(name = "priority",columnDefinition = "TINYINT")
    private boolean priority;

    public String licensePlateNumber() {
        return licensePlateNumber;
    }

    public boolean priority() {
        return priority;
    }

    protected LicensePlate(){

    }
    public LicensePlate(Driver driver,String licensePlateNumber,boolean priority){
        this.driver = driver;
        this.licensePlateNumber = licensePlateNumber;
        this.priority = priority;
    }

    public LicensePlateDto convert() {
        LicensePlateDto licensePlateDto = new LicensePlateDto();
        licensePlateDto.setGuid(this.guid());
        licensePlateDto.setLicensePlateNumber(this.licensePlateNumber);
        licensePlateDto.setPriorityUsed(this.priority);
        return licensePlateDto;
    }

    public static LicensePlate createLicensePlate(LicensePlateDto licensePlateDto) {
        LicensePlate licensePlate = new LicensePlate();
        licensePlate.priority = licensePlateDto.isPriorityUsed();
        licensePlate.licensePlateNumber = licensePlateDto.getLicensePlateNumber();
        return licensePlate;
    }

    public void createLicensePlate(Driver driver) {
        this.driver = driver;
    }

    public void updatePriorityLicense(LicensePlate prePriorityLicensePlate) {
        if(prePriorityLicensePlate != null){
            prePriorityLicensePlate.priority = false;
        }
        this.priority = true;
    }
}
